import { AccountRegion, CurrencySymbol } from '@/enums'
import { Decimals } from '@/utils/decimal.ts'
import { useAppStore } from '@/store'

export const getPrecision = (value: any, currencySymbol: any, precision: any) => {
	const vnOrIdn = currencySymbol === CurrencySymbol.vn || currencySymbol === CurrencySymbol.idn

	// 最少小数位: VND或IDN默认没有小数位, 其他币种始终显示最小2位小数
	let minimumFractionDigits = vnOrIdn ? 0 : 2
	// 最多小数位: (VND或IDN && 金额<1) || 金额 < 0.01 ? 最多4位小数 : 最多2位小数
	// let maximumFractionDigits = (vnOrIdn && value < 1) || value < 0.01 ? 4 : vnOrIdn ? 0 : 2

	// 最多小数位: 4 ; (所有金额 && 所有币种)
	// let maximumFractionDigits = 4

	// 最多小数位: 金额 < 1 ? 最多4位小数 : 最多2位小数
	let maximumFractionDigits = value < 1 ? 4 : vnOrIdn ? 0 : 2

	// 手动传入精度,覆盖币种逻辑
	if (precision !== undefined) {
		if (typeof precision === 'function') {
			const res = precision({
				value,
				currencySymbol,
				minimumFractionDigits,
				maximumFractionDigits
			})
			minimumFractionDigits = res[0]
			maximumFractionDigits = res[1]
		} else if (Array.isArray(precision)) {
			precision[0] !== true && (minimumFractionDigits = precision[0])
			precision[1] !== true && (maximumFractionDigits = precision[1])
		} else {
			minimumFractionDigits = precision
			maximumFractionDigits = precision
		}
	}
	return [minimumFractionDigits, maximumFractionDigits]
}

function _fmtCurrency(value: any, currencySymbol: any, precision: any) {
	// const vnOrIdn = currencySymbol === CurrencySymbol.vn || currencySymbol === CurrencySymbol.idn
	//
	// // 最少小数位: VND或IDN默认没有小数位, 其他币种始终显示最小2位小数
	// let minimumFractionDigits = vnOrIdn ? 0 : 2
	// // 最多小数位: (VND或IDN && 金额<1) || 金额 < 0.01 ? 最多4位小数 : 最多2位小数
	// // let maximumFractionDigits = (vnOrIdn && value < 1) || value < 0.01 ? 4 : vnOrIdn ? 0 : 2
	//
	// // 最多小数位: 4 ; (所有金额 && 所有币种)
	// // let maximumFractionDigits = 4
	//
	// // 最多小数位: 金额 < 1 ? 最多4位小数 : 最多2位小数
	// let maximumFractionDigits = value < 1 ? 4 : vnOrIdn ? 0 : 2
	//
	// // 手动传入精度,覆盖币种逻辑
	// if (precision !== undefined) {
	// 	minimumFractionDigits = precision
	// 	maximumFractionDigits = precision
	// }
	const digits = getPrecision(value, currencySymbol, precision)
	const numberFormat = new Intl.NumberFormat('en-US', {
		minimumFractionDigits: digits[0],
		maximumFractionDigits: digits[1],
		// 金额按照要求的小数位截断显示,不要四舍五入
		roundingMode: 'trunc'
	} as any)
	return numberFormat.format(+value)
}

export function fmtNo(value: string | undefined) {
	let res = ''
	if (value) {
		res = value.slice(-16)
	}
	return res
}

export const encryptAddress = (
	address: string | undefined | any,
	prefixLength: number = 3,
	suffixLength: number = 3
) => {
	if (typeof address !== 'string' || address.length <= prefixLength + suffixLength) {
		return address
	}
	const prefix = address.slice(0, prefixLength)
	const suffix = address.slice(-suffixLength)
	const mask = '*'.repeat(Math.max(4, address.length - prefixLength - suffixLength))
	return `${prefix}${mask}${suffix}`
}

export function fmtMobile(
	mobile: string | undefined | any,
	prefixLength: number = 3,
	suffixLength: number = 3
) {
	if (mobile) {
		const [_s, value] = mobile.split(' ')
		const prefix = (value || _s).slice(0, prefixLength)
		const suffix = (value || _s).slice(-suffixLength)
		return `${value ? _s + ' ' : ''}${prefix}***${suffix}`
	} else {
		return mobile
	}
}

export function fmtEmail(email: string | undefined | any) {
	if (email) {
		const [value, _s] = email.split('@')
		if (value.length === 1) {
			return '*@' + _s
		}
		const prefix = value.slice(0, 1)
		return prefix + '***' + '@' + _s
	} else {
		return email
	}
}

export function fmtAmount(
	val: any,
	{
		currency,
		currencySymbol,
		/* 显示货币的 国际简称 而不是 符号; */
		useName = false,
		hideCurrenySymbol = false,
		precision
	}: any = {}
) {
	const appStore = useAppStore()
	currency = appStore.currency
	currencySymbol = appStore.currencySymbol
	if (val == null) {
		return ''
	}
	let isNegative = false
	if (typeof val === 'string' && val.indexOf('-') === 0) {
		isNegative = true
	} else if (typeof val === 'number' && val < 0) {
		isNegative = true
	}
	return (
		(isNegative ? '-' : '') +
		(hideCurrenySymbol ? '' : useName ? currency + ' ' : currencySymbol) +
		_fmtCurrency(Math.abs(val), currencySymbol, precision)
	)
}

const numberFormat = new Intl.NumberFormat('en-US', {
	minimumFractionDigits: 0,
	maximumFractionDigits: 0
})

export function fmtNum(value: any) {
	return numberFormat.format(+value)
}

export function amountToK(value: any, precision: number) {
	const twoDigits = new Intl.NumberFormat('en-US', {
		minimumFractionDigits: 2,
		maximumFractionDigits: 2,
		// 金额按照要求的小数位截断显示,不要四舍五入
		roundingMode: 'trunc'
	} as any)
	const oneDigit = new Intl.NumberFormat('en-US', {
		minimumFractionDigits: 1,
		maximumFractionDigits: 1,
		// 金额按照要求的小数位截断显示,不要四舍五入
		roundingMode: 'trunc'
	} as any)
	const appStore = useAppStore()
	const currencySymbol = appStore.currencySymbol
	const vnOrIdn =
		currencySymbol === AccountRegion.vn.currencySymbol ||
		currencySymbol === AccountRegion.idn.currencySymbol
	const amount = vnOrIdn ? Number(value) : Number(Math.round(value * 100) / 100)
	const si = [
		{ value: 1, symbol: '' },
		{ value: 1e3, symbol: 'K' },
		{ value: 1e6, symbol: 'M' },
		{ value: 1e9, symbol: 'B' }
	]
	if (amount < 10000) {
		if (vnOrIdn || precision === 0) {
			return parseInt(amount + '').toLocaleString()
		}
		return amount < 1000 ? twoDigits.format(amount) : oneDigit.format(amount)
	}
	let i
	for (i = si.length - 1; i > 0; i--) {
		if (amount >= si[i].value * 10) {
			break
		}
	}
	const result = amount / si[i].value
	const finnalResult =
		(result < 1000 ? twoDigits.format(result) : oneDigit.format(result)) + si[i].symbol
	return finnalResult
}

const oddsFormat = new Intl.NumberFormat('en-US', {
	minimumFractionDigits: 2,
	maximumFractionDigits: 2,
	roundingMode: 'trunc'
} as any)

export function fmtOdds(value: string | number, showSymbol = false) {
	const odds = oddsFormat.format(+value)
	return (showSymbol ? '@' : '') + odds
}

/**
 *
 * @param {number,string} value
 * @param {boolean} hundred
 * @param {number,string,number[]} precision
 * @returns {string}
 */
export const percentage = (value: any, hundred = false, precision = 'gte100') => {
	if (!value && value !== 0) return ''
	const _value = Decimals.mul(value, hundred ? 100 : 1)
	// precision: isInteger 整数不显示小数位; gte100 大于一百不显示小数位
	let minimumFractionDigits = Number.isInteger(_value) ? 0 : 2
	let maximumFractionDigits: number = 2
	// 手动传入精度,覆盖币种逻辑
	if (Array.isArray(precision)) {
		precision[0] !== true && (minimumFractionDigits = precision[0])
		precision[1] !== true && (maximumFractionDigits = precision[1])
	} else if (precision === 'integer') {
		minimumFractionDigits = Number.isInteger(_value) ? 0 : 2
		maximumFractionDigits = 2
	} else if (precision === 'gte100') {
		minimumFractionDigits = _value > 100 ? 0 : 2
		maximumFractionDigits = 2
	} else {
		minimumFractionDigits = Number(precision)
		maximumFractionDigits = Number(precision)
	}
	const numberFormat = new Intl.NumberFormat('en-US', {
		minimumFractionDigits,
		maximumFractionDigits,
		// 金额按照要求的小数位截断显示,不要四舍五入
		roundingMode: 'trunc'
	} as any)
	return numberFormat.format(_value) + '%'
}

export function toAmount(value: any) {
	const result = Number(value) || 0
	return String(result).replace(/(\d)(?=(\d{3})+($|\.))/g, '$1,')
}

export function fmtTextWithHtml(html: string) {
	const div = document.createElement('div')
	div.innerHTML = html
	const text = div.textContent || div.innerText || ''
	return text.replace(/\s+/g, ' ').trim()
}
